package com.jane.gzzb;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.jane.fragment.CategoryFragment;
import com.jane.fragment.HomePageFragment;
import com.jane.fragment.HostHeadlinesFragment;
import com.jane.fragment.MineFragment;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private ArrayList<Fragment> fragments;
    private ArrayList<RadioButton> radioButtons;
    private RadioGroup rg_main;
    private FragmentManager manager;
    private int currentIndex;
    private int targetIndex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        setListener();
    }

    private void initView() {
        rg_main= (RadioGroup) findViewById(R.id.rg_main);
    }

    private void initData() {
        fragments=new ArrayList<>();
        radioButtons=new ArrayList<>();
        //初始化RadioButton集合
        for (int i = 0; i <rg_main.getChildCount(); i++) {

            RadioButton radioButton= (RadioButton) rg_main.getChildAt(i);
            Drawable[] drawables = radioButton.getCompoundDrawables();
            //获取手机屏幕的密度比例
            float scale = getResources().getDisplayMetrics().density;
            //将密度比例转化成像素
            int width = (int) (18* scale + 0.5f);
            if (drawables[1] != null) {
                //将drawable的整个部分都限制在了bounds规定的区域内
                drawables[1].setBounds(0,0, width,width);
                //按drawable.setBound()设置的宽高,绘制drawable
                //1.上2.下3.左4.右
                radioButton.setCompoundDrawables(null, drawables[1],null,null);
            }
            radioButtons.add(radioButton);
        }

        //创建碎片并添加到碎片集合
        HomePageFragment homePageFragment=new HomePageFragment();//首页
        CategoryFragment categoryFragment=new CategoryFragment();//栏目分类
        HostHeadlinesFragment hostHeadlinesFragment=new HostHeadlinesFragment();//主播头条
        MineFragment mineFragment=new MineFragment();//我的

        fragments.add(homePageFragment);
        fragments.add(categoryFragment);
        fragments.add(hostHeadlinesFragment);
        fragments.add(mineFragment);

        //添加首页碎片最为第一个显示的碎片
        getSupportFragmentManager().beginTransaction().add(R.id.main_ll_fragment,homePageFragment).commit();
        currentIndex=0;
        radioButtons.get(0).setChecked(true);
        radioButtons.get(0).setTextColor(Color.parseColor("#FF6F4D"));
    }

    private void setListener() {
        rg_main.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        //设置目的碎片的id及底部文字颜色
        for (int i = 0; i <radioButtons.size(); i++) {
            if(radioButtons.get(i).getId()==checkedId){
                targetIndex=i;
                //设置文本的颜色
                radioButtons.get(i).setTextColor(Color.parseColor("#FF6F4D"));
            }else{
                radioButtons.get(i).setTextColor(Color.parseColor("#999999"));
            }
        }
        changeFragment();
    }
    //切换碎片的方法
    private void changeFragment() {
        manager=getSupportFragmentManager();
        FragmentTransaction transaction=manager.beginTransaction();
        Fragment currFrag=fragments.get(currentIndex);
        Fragment tarFrag=fragments.get(targetIndex);
        if(targetIndex!=currentIndex){
            if(tarFrag.isAdded()){
                transaction.hide(currFrag);
                transaction.show(tarFrag);
            }else{
                transaction.hide(currFrag);
                transaction.add(R.id.main_ll_fragment,tarFrag);
            }
        }
        transaction.commit();
        currentIndex=targetIndex;
    }
}
